package com.huobao.yunying.activitys;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.OpenBrowUtils;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.zhangying.R;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Act_Kefu extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.tv_1)
	private TextView tv_tel;
	@ViewInject(R.id.tv_2)
	private TextView tv_qq;
	@ViewInject(R.id.listview)
	private ListView listview;
	private ArrayAdapter<String> adapter;

	@ViewInject(R.id.img_kefu)
	private ImageView img_kefu;

	@ViewInject(R.id.view_kefu)
	private View view_kefu;

	@Override
	protected int getContentView() {
		return R.layout.layout_kefu;
	}

	@Override
	protected String getTitleText() {
		return "客服中心";
	}

	@Override
	protected void init() {
		listview.setOnItemClickListener(this);
		adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listStrings);
		listview.setAdapter(adapter);
		tv_tel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (phone_mob == null)
					return;
				Uri uri = Uri.parse("tel:" + phone_mob);
				Intent intent = new Intent(Intent.ACTION_DIAL, uri);
				startActivity(intent);
			}
		});
		getData();
		getListData();
		view_kefu.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View view) {
				SkipUtils.toChat(Act_Kefu.this, null);
			}
		});
	}

	String phone_mob;

	private void getData() {
		Map<String, String> map = HttpUtil.getParams("app", "news", "act", "custome");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				phone_mob = jsonObject.getString("phone_mob");
				String im_qq = jsonObject.getString("im_qq");
				tv_tel.setText(phone_mob);
				tv_qq.setText(im_qq);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	private List<Link> list = new ArrayList<Link>();
	private List<String> listStrings = new ArrayList<String>();
	private void getListData() {
		Map<String, String> map = HttpUtil.getParams("app", "news", "act", "consultation");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				for(int i=0;i<array.size();i++){
					JSONObject object = array.getJSONObject(i);
					Link link = new Link();
					link.article_id = object.getString("article_id");
					link.title = object.getString("title");
					link.wx_url = object.getString("wx_url");
					list.add(link);
					listStrings.add(link.title);
				}
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}
	
	class Link{
		String article_id;
		String title;
		String wx_url;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Link link = list.get(position);
		OpenBrowUtils.startBrower(context, link.wx_url);
	}
}
